package vsm.vsm_server.mapper;

import org.apache.ibatis.annotations.*;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.pojo.User.User;

import java.math.BigInteger;
import java.util.List;

@Mapper
public interface UserMapper {
    /*
    查询全部用户
     */
    @Select("select * from user")
    List<User> list();
    /*
    根据id删除用户???
     */
    @Update("update user set isdelete=true where userId=#{id}")
    boolean deleteById(BigInteger id);
    /*
    创建用户
     */
    @Insert("insert into user (userName,userPwd,userInfo,avatar,fans,subscriber) values(#{userName},#{userPwd},#{userInfo},#{avatar},#{fans},#{subscriber})")
    @Options(useGeneratedKeys=true, keyProperty="userId", keyColumn="userId")
    void create(User newuser);
    /*
    根据id获取用户
     */
    @Select("select * from user where userId=#{userId} and isdelete = false")
    User getById(BigInteger userId);
    /*
    根据id获取用户主要信息
     */
    @Select("select userId,userName,userInfo,avatar from user where userId=#{userId} and isdelete = false")
    UserInfo getInfoById(BigInteger userId);
    /*
    根据id获取用户头像id
     */
    @Select("select avatar from user where userId=#{userId} and isdelete = false")
    String getAvatarId(BigInteger userId);
    /*
   根据name获取用户
    */
    @Select("select * from user where userName=#{userName} and isdelete = false")
    User getByName(String userName);
    /*
   根据name模糊搜索用户
    *//*
    @Select("select * from user where userId=#{userId}")
    List<User> searchAboutName(String userId);*/
    /*
    根据id更新用户
     */
    @Update("update user set userName=#{userName},userInfo=#{userInfo},avatar=#{avatar} where userId=#{userId}")
    Boolean update(User newuser);
    /*
    增加粉丝数
     */
    @Update("update user set fans = fans + 1 where userId = #{userId}")
    void addFans(BigInteger userId);
    /*
   减少粉丝数
    */
    @Update("update user set fans = fans - 1 where userId = #{userId}")
    void deFans(BigInteger userId);
    /*
    增加订阅数
    */
    @Update("update user set subscriber = subscriber + 1 where userId = #{userId}")
    void addSubs(BigInteger userId);
    /*
    减少订阅数
    */
    @Update("update user set subscriber = subscriber - 1 where userId = #{userId}")
    void deSubs(BigInteger userId);
    /*
    校验粉丝数量
     */
    @Update("update user set fans = #{fans} ,subscriber = #{subscribes} where userId = #{userId}")
    void verifyFans(BigInteger userId, Integer fans, Integer subscribes);

    @Select("""
    <script>
      SELECT userId,userName,userInfo,avatar
      FROM   user
      WHERE  userId IN
      <foreach collection="userIds" item="id" open="(" separator="," close=")">
        #{id}
      </foreach>
    </script>
""")
    List<UserInfo> getUserInfoList(@Param("userIds")List<BigInteger> userIdList);

    @Select("select * from user   WHERE  (username LIKE #{keyword} or userid LIKE #{keyword} )AND isdelete = false")
    List<UserInfo> findUsersByKeyword(String keyword);

}
